##################################################################################################################
# Code for Results in Appendix Section A1.1
##################################################################################################################
write.table(data.frame(Description=c("","","","Appendix Section A1.1: Indirect Evidence From Wisconsin Lobbying Disclosures")),row.names=F,col.names=F,quote=F)

##################################################################################################################
# To run, please set the working directory to match the location of the folder containing all replication files.
##################################################################################################################
#setwd("Replication_PSRM_ST24")

#if not yet created, create "output" folder
if("output"%in%list.files()==F){dir.create("output")}

#clear environment
rm(list=ls());gc();gc();gc();gc()

#if not yet installed, install packages
if(length(find.package("dplyr",quiet=T))==0){install.packages("dplyr")}
if(length(find.package("DescTools",quiet=T))==0){install.packages("DescTools")}

#Load packages
library(dplyr)
library(DescTools)

#Set POSIX locale
Sys.setlocale(locale="C")

#Load main data set
loball5ciA<-readRDS("Data/PSRM_main_dataset.rds")

#Show presence of round number payments (w/ no payment in second half-year) or 
# repeating numbers in given year by client-lobbying company /employer 
# ( aggregating in-house lobbyists with client as employer)

loball5ciA_lobcomp<-loball5ciA %>%
  filter(type!="nonlobbyist") %>%
  group_by(principal_cleaned,lobbying_company_cleaned,type,halfyear,year=substr(halfyear,1,4)) %>%
  summarise(.groups="keep",comparable_total_amounts_cleaned_exp=sum(comparable_total_amounts_cleaned_exp,na.rm=T),
            comparable_total_amounts_cleaned_noexp=sum(comparable_total_amounts_cleaned_noexp,na.rm=T))

#Two indicators: 
#First, same non-zero expenditure number repeating across half-years
loball5ciA_lobcomp$cond_repeat<-0
loball5ciA_lobcomp$cond_repeat[intersect(which(loball5ciA_lobcomp$comparable_total_amounts_cleaned_exp!=0),
                                         c(which(duplicated(loball5ciA_lobcomp[c("principal_cleaned","lobbying_company_cleaned","type","year","comparable_total_amounts_cleaned_exp")],fromLast = T)==T),
                                           which(duplicated(loball5ciA_lobcomp[,c("principal_cleaned","lobbying_company_cleaned","type","year","comparable_total_amounts_cleaned_exp")],fromLast = F)==T)))]<-1
#Second, round number (i.e., divisible by 1000) & only one payment per year
loball5ciA_lobcomp$cond_round1000<-0
loball5ciA_lobcomp$cond_round1000[which(loball5ciA_lobcomp$comparable_total_amounts_cleaned_exp%%1000==0&loball5ciA_lobcomp$comparable_total_amounts_cleaned_exp!=0)]<-1
loball5ciA_lobcomp$zero<-as.numeric(loball5ciA_lobcomp$comparable_total_amounts_cleaned_exp==0)

#Aggregating to yearly level
loball5ciA_lobcomp2<-loball5ciA_lobcomp %>%
  group_by(principal_cleaned,lobbying_company_cleaned,type,year=substr(halfyear,1,4)) %>%
  summarise(.groups="keep",cond_repeats=sum(cond_repeat),cond_round1000s=sum(cond_round1000),zero=sum(zero))

#Creating indicator for presence of flat fees 
#Looking at number of client's yearly observations (by lobbying employer, i.e. lobbying company or client, for in-house lobbyists)
# where payment amounts either repeat exactly, or 
# there is one non-zero amount with a round number and one zero amount
loball5ciA_lobcomp2$cond_either1000s1<-as.numeric(loball5ciA_lobcomp2$cond_round1000s==1&loball5ciA_lobcomp2$zero==1|loball5ciA_lobcomp2$cond_repeats>0)

#Result (need to exclude those years with two zero expenditures)
write.table(data.frame(Description=c("","","","First set of results:",
  "Presence of `flat fee indicator' for yearly client-lobbying employer observations,",
  "where the lobbying employer is lobbying firm for contract lobbyist and client for in-house lobbyist.",
  "(Travel and living expenses for in-house lobbyists are included in expenditures.)","")),row.names=F,col.names=F,quote=F)
print(PercTable(loball5ciA_lobcomp2$cond_either1000s1[which(loball5ciA_lobcomp2$zero!=2)],loball5ciA_lobcomp2$type[which(loball5ciA_lobcomp2$zero!=2)],rfrq="001"))
#             contract   inhouse
# 
# 0   freq       6'321     4'221
#     p.col      74.2%     98.7%
#   
# 1   freq       2'199        57
#     p.col      25.8%      1.3%

#Repeat process using lobbyist compensation without travel and living expenses attributed to in-house lobbyists
loball5ciA_lobcomp$cond_repeat_ne<-0
loball5ciA_lobcomp$cond_repeat_ne[intersect(which(loball5ciA_lobcomp$comparable_total_amounts_cleaned_noexp!=0),
                                            c(which(duplicated(loball5ciA_lobcomp[c("principal_cleaned","lobbying_company_cleaned","type","year","comparable_total_amounts_cleaned_noexp")],fromLast = T)==T),
                                              which(duplicated(loball5ciA_lobcomp[,c("principal_cleaned","lobbying_company_cleaned","type","year","comparable_total_amounts_cleaned_noexp")],fromLast = F)==T)))]<-1
loball5ciA_lobcomp$cond_round1000_ne<-0
loball5ciA_lobcomp$cond_round1000_ne[which(loball5ciA_lobcomp$comparable_total_amounts_cleaned_noexp%%1000==0&loball5ciA_lobcomp$comparable_total_amounts_cleaned_noexp!=0)]<-1
loball5ciA_lobcomp$zero_ne<-as.numeric(loball5ciA_lobcomp$comparable_total_amounts_cleaned_noexp==0)

loball5ciA_lobcomp3<-loball5ciA_lobcomp %>%
  group_by(principal_cleaned,lobbying_company_cleaned,type,year=substr(halfyear,1,4)) %>%
  summarise(.groups="keep",cond_repeats=sum(cond_repeat_ne), cond_round1000s=sum(cond_round1000_ne),zero=sum(zero_ne))

loball5ciA_lobcomp3$cond_either1000s1<-as.numeric(loball5ciA_lobcomp3$cond_round1000s==1&loball5ciA_lobcomp3$zero==1|loball5ciA_lobcomp3$cond_repeats>0)

#look at number of client's yearly observations (by lobbying employer) where payment amounts either repeat exactly, or 
# there is one non-zero amount with a round number and one zero amount

write.table(data.frame(Description=c("","","Second set of results:",
"Presence of `flat fee indicator' for yearly client-lobbying employer observations,",
"where the lobbying employer is lobbying firm for contract lobbyist and client for in-house lobbyist.",
"(Travel and living expenses for in-house lobbyists are excluded from expenditures.)")),row.names=F,col.names=F,quote=F)
#Results (excluding those years with two zero expenditures):
print(PercTable(loball5ciA_lobcomp3$cond_either1000s1[which(loball5ciA_lobcomp3$zero!=2)],loball5ciA_lobcomp3$type[which(loball5ciA_lobcomp3$zero!=2)],rfrq="001"))
#             contract   inhouse
# 
# 0   freq       6'321     4'177
#     p.col      74.2%     97.9%
#   
# 1   freq       2'199        91
#     p.col      25.8%      2.1%

